﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Schedule.UI.ScheduleService;
using Schedule.UI.Foundation;
using System.Windows.Input;

namespace Schedule.UI.ViewModel
{
    public class ClassViewModel : INotifyPropertyChanged
    {
        public ClassViewModel()
        {
            RefreshLophoc();
            this.PropertyChanged += (s, e) => {
                if (insertCurLophoc != null)
                    insertCurLophoc.RaiseCanExecuteChanged();
                if (saveCurLophoc != null)
                    saveCurLophoc.RaiseCanExecuteChanged();
                if (delCurLophoc != null)
                    delCurLophoc.RaiseCanExecuteChanged();
            };
            
        }
        #region Khai bao
        public ScheduleServiceClient ServiceClient = new ScheduleServiceClient();
        #region propchange
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
        private IEnumerable<LOPHOC> lophocs;
        public IEnumerable<LOPHOC> Lophocs
        {
            get { return this.lophocs; }
            set
            {
                this.lophocs = value;
                OnPropertyChange("Lophocs");
            }
        }


        private LOPHOC currentLophoc;
        public LOPHOC CurrentLophoc
        {
            get { return this.currentLophoc; }
            set
            {
                this.currentLophoc = value;
                OnPropertyChange("CurrentLophoc");
            }
        }
        private IEnumerable<LOAIPHONG> loaiphongs;
        public IEnumerable<LOAIPHONG> Loaiphongs
        {
            get { return this.loaiphongs; }
            set
            {
                this.loaiphongs = value;
                OnPropertyChange("Loaiphongs");
            }
        }
        
        private LOAIPHONG currentLoaiPhong = new LOAIPHONG();
        public LOAIPHONG CurrentLoaiPhong 
        {
            get { return this.currentLoaiPhong; }
            set
            {
                this.currentLoaiPhong = value;
                OnPropertyChange("CurrentLoaiPhong");
            }
        }
       
        #endregion

        #region refreshPhong
        private void RefreshLophoc()
        {
            this.ServiceClient.getAllLophocCompleted += (s, e) =>
            {
                this.Lophocs = e.Result;
            };
            this.ServiceClient.getAllLophocAsync();
        }

        private void RefreshLoaiPhong()
        {
            //throw new NotImplementedException();
            this.ServiceClient.getAllLoaiPhongCompleted += (s, e) =>
            {
                this.loaiphongs = e.Result;
            };
            this.ServiceClient.getAllLoaiPhongAsync();
            

        }
        #endregion
        
        #region Save
        private RelayCommand saveCurLophoc;
        public ICommand SaveCurLophoc
        {
            get { return saveCurLophoc ?? (saveCurLophoc = new RelayCommand(() => SaveLophoc(), () => this.currentLophoc != null && this.currentLophoc.ID != 0)); }
        }

        private void SaveLophoc()
        {
            ServiceClient.saveLophocAsync(currentLophoc);
            RefreshLophoc();
        }

        
        #endregion

        #region del
        private RelayCommand delCurLophoc;
        public ICommand DelCurLophoc
        {
            get { return delCurLophoc ?? (delCurLophoc = new RelayCommand(() => DelLophoc(), () => this.currentLophoc != null && this.currentLophoc.ID != 0)); }
        }

        private void DelLophoc()
        {
            ServiceClient.deleteLophocAsync(currentLophoc);
            RefreshLophoc();
        }
        #endregion

        #region Insert

        private RelayCommand insertCurLophoc;
        public ICommand InsertCurLophoc
        {
            get { return insertCurLophoc ?? (insertCurLophoc = new RelayCommand(() => InsertLophoc(), () => this.currentLophoc != null)); }
        }

        private void InsertLophoc()
        {
            ServiceClient.insertLophocAsync(CurrentLophoc);
            RefreshLophoc();
        }
        #endregion
        
    }
}
